﻿package base
{
	import com.dusk.game.HackChecker;
	import com.dusk.util.ResUtil;
	import com.game.engine.MainGame;
	import com.game.event.GameEventType;
	import com.game.manager.EventManager;
	import com.game.manager.GameSceneManager;
	import com.greensock.TweenMax;
	
	import config.Config;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	public class BaseLevelListener
	{
		
		public function BaseLevelListener()
		{
			super();
		}
		
		private var waitForRegisterSprite:Sprite;
		private var blackBg:Sprite;
		
		public function get gc():Config
		{
			return Config.getIns();
		}
		
		public function init():void
		{
			waitForRegisterSprite = ResUtil.getNewObj("waitForBitmapDataRegister") as Sprite;
			GameSceneManager.getIns().addScene(waitForRegisterSprite,"top");
			TweenMax.delayedCall(0.1, start);
		}
		
		public function start():void
		{
			if (GameSceneManager.getIns().topLayer.contains(waitForRegisterSprite))
			{
				TweenMax.to(waitForRegisterSprite, 0.6, {"alpha": 0});
				TweenMax.delayedCall(0.7, function (param1:Sprite):*
				{
					if (param1.parent)
					{
						param1.parent.removeChild(param1);
					}
				}, [waitForRegisterSprite]);
			}
			gc.pWorld.pWorldStart();
			for each (var bh:BaseHero in gc.team.getHeroArray())
			{
				if (HackChecker.checkAttribute(bh))
				{
					HackChecker.hackHandler();
				}
			}
		}
		
		public function keyBoardDownForW(param1:BaseHero):void
		{
			if (!MainGame.getIns().battleData.levelCleared)
			{
				MainGame.getIns().battleData.levelCleared = true;
				TweenMax.to(gc.gameInfo, 1, {
					"alpha": 0
				});
				TweenMax.to(gc.gameScene, 1, {
					"alpha"     : 0,
					"onComplete": function ():*
					{
						EventManager.getIns().dispatchEvent(GameEventType.GAME_VICTOR);
					}
				});
			}
		}
		
		public function step():void
		{
			drawBlackBG();
		}
		
		public function startBlackBG():void
		{
			if (!blackBg)
			{
				blackBg = new Sprite();
				blackBg.blendMode = "layer";
				gc.gameInfo.addChildAt(blackBg, 0);
			}
		}
		
		public function stopBlackBG():void
		{
			if (blackBg)
			{
				blackBg.graphics.clear();
				while (blackBg.numChildren > 0)
				{
					blackBg.removeChildAt(0);
				}
				if (gc.gameInfo)
				{
					if (gc.gameInfo.contains(blackBg))
					{
						gc.gameInfo.removeChild(blackBg);
					}
				}
				blackBg = null;
			}
		}
		
		public function destroy():void
		{
			stopBlackBG();
			var i:* = waitForRegisterSprite.getChildAt(0);
			if (i is Bitmap)
			{
				i.bitmapData.dispose();
			}
			waitForRegisterSprite = null;
			blackBg = null;
		}
		
		private function drawBlackBG():void
		{
			if (blackBg)
			{
				while (blackBg.numChildren > 0)
				{
					blackBg.removeChildAt(0);
				}
				blackBg.graphics.clear();
				blackBg.graphics.beginFill(0, 1);
				blackBg.graphics.drawRect(0, 0, 940, 590);
				blackBg.graphics.endFill();
				for each (var bh:BaseHero in gc.team.getHeroArray())
				{
					var p1:Point = gc.gameScene.localToGlobal(new Point(bh.x, bh.y - 50));
					var sprite:Sprite = ResUtil.getNewObj("ViewMask") as Sprite;
					sprite.blendMode = "erase";
					sprite.x = p1.x;
					sprite.y = p1.y;
					blackBg.addChild(sprite);
				}
				var p2:Point = gc.gameInfo.localToGlobal(new Point(gc.gameInfo.x, gc.gameInfo.y));
				blackBg.x = p2.x;
				blackBg.y = 0;
			}
		}
	}
}
